﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace BrainTechLLC.Silverlight.ClientApplication.Controls
{
    public partial class TextNotificationsControl : UserControl
    {
        FontFamily font = new FontFamily("Verdana");
        double standardSize = 11;
        string _lastSaved;

        public TextNotificationsControl()
        {
            // Required to initialize variables
            InitializeComponent();
            Loaded += new RoutedEventHandler(TextNotificationsControl_Loaded);
        }

        void TextNotificationsControl_Loaded(object sender, RoutedEventArgs e)
        {
            txtNotifications.FontSize = standardSize;
            txtNotifications.FontFamily = font;
        }

        public void AppendNotification(string s)
        {
            App.MainDispatcher.BeginInvoke(() =>
                {
                    if (s.Length > 4000)
                    {
                        string chunk = s.Substring(s.Length - 4000, 4000);
                        HandleAppend(chunk);                                                
                    }
                    else
                    {
                        HandleAppend(s);
                    }                    
                });
        }

        public void AppendNotificationColor(string s, Color foreground, Color background)
        {
            App.MainDispatcher.BeginInvoke(() => AppendNotificationTextColor(s, foreground, background));
        }

        public void HandleAppend(string s)
        {
            if (txtNotifications.IsEnabled)
            {
                StoredState oldState = null;

                var chars = s.ToCharArray();
                int charsLength = chars.Length;
                Color newForeground = Colors.Black;
                bool newColors;
                string lastString = "\n";
                //txtNotifications.SelectRange(CursorPosition.End, txtNotifications.LineItems.Count, 0);
                txtNotifications.FontSize = standardSize;
                txtNotifications.FontFamily = font;
                txtNotifications.FontBackground = Colors.White;
                txtNotifications.FontForeground = Colors.Black;

                for (int n = 0; n < charsLength; n++)
                {
                    char chr = chars[n];
                    newColors = false;
                    if (chr == '{')
                    {
                        if (n + 1 < charsLength)
                        {
                            n++;
                            char c = chars[n];
                            if (c == 'b') { newForeground = Colors.Blue; newColors = true; }
                            else if (c == 'B') { newForeground = Colors.Blue; newColors = true; }
                            else if (c == 'g') { newForeground = Colors.Green; newColors = true; }
                            else if (c == 'G') { newForeground = Colors.Green; newColors = true; }
                            else if (c == 'c') { newForeground = Colors.Cyan; newColors = true; }
                            else if (c == 'C') { newForeground = Colors.Green; newColors = true; }
                            else if (c == 'w') { newForeground = Colors.LightGray; newColors = true; }
                            else if (c == 'W') { newForeground = Colors.White; newColors = true; }
                            else if (char.ToUpper(c) == 'N') { newForeground = Colors.Black; newColors = true; }
                            else if (c == 'p') { newForeground = Colors.Purple; newColors = true; }
                            else if (c == 'P') { newForeground = Colors.Purple; newColors = true; }
                            else if (c == 'd') { newForeground = Colors.Orange; newColors = true; }
                            else if (c == 'r') { newForeground = Colors.Red; newColors = true; }
                            else if (c == 'R') { newForeground = Colors.Red; newColors = true; }
                            else if (c == 'Y') { newForeground = Colors.Yellow; newColors = true; }
                            else if (c == 'y') { newForeground = Colors.Brown; newColors = true; }
                            else if (c == '}') { n--; }
                            else if (c == '{') { n--; }

                            if (n + 1 < charsLength && chars[n + 1] == '}')
                            {
                                n++;
                            }
                        }
                    }
                    else
                    {
                        lastString += chr;
                    }
                    if (newColors)
                    {
                        if (oldState == null)
                            oldState = StoredState.CreateSavedState(txtNotifications);

                        if (!string.IsNullOrEmpty(lastString))
                        {                            
                            txtNotifications.InsertString(lastString, null);
                            lastString = string.Empty;
                        }
                        txtNotifications.FontForeground = newForeground;
                    }
                }

                if (!string.IsNullOrEmpty(lastString))
                    txtNotifications.InsertString(lastString, null);

                if (oldState != null)
                    oldState.Restore(txtNotifications);
            }
        }

        protected void AppendNotificationTextColor(string s, Color foreground, Color background)
        {
            try
            {
                if (txtNotifications.IsEnabled)
                {
                    txtNotifications.FontSize = standardSize;
                    txtNotifications.FontFamily = font;

                    StoredState oldState = StoredState.CreateSavedState(txtNotifications);

                    txtNotifications.FontForeground = foreground;
                    txtNotifications.FontBackground = background;
                    txtNotifications.FontSize = 12;

                    string toAppend = "\n --> " + s;
                    txtNotifications.InsertString(toAppend, null);
                    
                    oldState.Restore(txtNotifications);

                    if (txtNotifications.LineItems.Count > 5000)
                    {
                        _lastSaved = txtNotifications.QueryText().Text;
                        txtNotifications.SelectRange(CursorPosition.Start, 0, txtNotifications.LineItems.Count - toAppend.Length);
                        txtNotifications.RemoveSelection();
                        txtNotifications.SelectRange(CursorPosition.End, txtNotifications.LineItems.Count, 0);
                    }
                }
            }
            catch { }
        }

        protected void AppendNotificationText(string s)
        {
            try
            {
                if (txtNotifications.IsEnabled)
                {
                    if (s.Length > 4000)
                    {
                        txtNotifications.InsertString(Environment.NewLine, null);

                        while (s.Length > 4000)
                        {
                            string chunk = s.Substring(0, 4000);
                            s = s.Substring(4000);

                            txtNotifications.InsertString(chunk, null);
                        }
                    }
                    else
                    {
                        txtNotifications.InsertString(Environment.NewLine + s, null);
                    }

                }
            }
            catch { }
        }
    }
}